package com.parovoz;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


import com.parovoz.R;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery.LayoutParams;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener, ViewSwitcher.ViewFactory {

	private ImageSwitcher mSwitcher;
	
	private Gallery gallery;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery_1);
        
        // Reference the Gallery view
        gallery = (Gallery) findViewById(R.id.gallery);
        // Set the adapter to our custom adapter (below)
        gallery.setAdapter(new ImageAdapter(this));
        
        //g.setOnItemSelectedListener(this);
        gallery.setOnItemClickListener(this);        
        
        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));
        mSwitcher.setOnClickListener(this);
        
        // We also want to show context menu for longpressed items in the gallery
        registerForContextMenu(gallery);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.add(R.string.gallery_2_text);
    }
    
    public View makeView() {
        ImageView i = new ImageView(this);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        return i;
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
        return true;
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        
        public ImageAdapter(Context c) {
            mContext = c;
            // See res/values/attrs.xml for the <declare-styleable> that defines
            // Gallery1.
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageResource(mImagePreviewIds[position]);
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            
            // The preferred Gallery item background
            i.setBackgroundResource(mGalleryItemBackground);
            
            return i;
        }

        private Context mContext;

    }
    
    private Integer[] mImageIds = {
            R.drawable.p_20120829_385124,
            R.drawable.p_20120904_385887,
            R.drawable.p_20120904_385978,
            R.drawable.p_20120904_385980
    };
    
    private Integer[] mImagePreviewIds = {
            R.drawable.p_20120829_385124_s,
            R.drawable.p_20120904_385887_s,
            R.drawable.p_20120904_385978_s,
            R.drawable.p_20120904_385980_s
    };
    
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		mSwitcher.setImageResource(mImageIds[arg2]);
		
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		mSwitcher.setImageResource(mImageIds[arg2]);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(gallery.isShown())
			gallery.setVisibility(View.INVISIBLE);
		else
			gallery.setVisibility(View.VISIBLE);
	}
}
